<?php
	class my_smarty extends Smarty{
		/**
		* Make registering smarty functions easier
		*/
		public function registerFunction($name, $funcname = ''){
			if ($funcname == ''){
				$funcname = $name;
			}
			$array = array(&$this, $funcname);
			$this->registerPlugin('function', $name, $array);		
		}
		
		/**
		* Make registering smarty blocks easier
		*/
		public function registerBlock($name){
			$array = array(&$this, $name);
			$this->registerPlugin('block', $name, $array);		
		}
		
		/**
		* Default options 
		*/
		public function __construct(){
			{
				$this->setCompileDir(constant('SMARTY_COMPILE_DIR'));
				//$this->setCacheDir(constant('SMARTY_CACHE_DIR'));
				//$this->caching = 1;
				$this->registerFunction('include_module');
				$this->registerFunction('formatDate');
				$this->registerFunction('notLoggedIn', 'isLoggedInElse');
				$this->registerBlock('isLoggedIn');
				$this->registerBlock('hasRights');
				$this->registerBlock('debugMode');
				$this->registerBlock('testMode');
			}
		}
		/**
		* {include_module} smarty call
		* 
		*/
		public function include_module($params){
			$controller = explode('/',$params['controller']);
			// take proper class name from controller name
			$controller_class_name = $controller[1];
			// if controller call has extension, remove it from class name
			if ($ext_start = strpos($controller_class_name, '.')){
				$controller_class_name = substr($controller_class_name, 0, $ext_start);
			}
			$module_path = constant('TEMPLATE_DIR').$params['controller'];
			// path to controller
			$controller_path = $module_path;
			// if hasn't extension, make him default
			if(strpos($module_path, '.') === false){
				$controller_path .= '.php';
			}
			// edit params for getContent function
			$params['controller'] = $controller_path;
			
			// if hasn't specified view, use default
			if(empty($params['view'])){
				$params['view'] = $module_path.'.tpl';
			} else {
				// else use it
				$view = $params['view'];
				// checks if file is declared in local or global scope
				// e.g. main vs. main/main
				if (strpos($view, $controller[0]) === false){
					$view = $controller[0].'/'.$view;	
				}
				// edit params for getContent function
				$params['view'] = constant('TEMPLATE_DIR').$view;
			}
			// finally load proper controller
			include_once($controller_path);
			$controller_class = new $controller_class_name;
			// and get template
			return $controller_class->getContent($params);
		}
		
		
		/**
		*	register else keyword for is logged in 
		*
		*/
		public function isLoggedInElse($params){
			return $this->left_delimiter . 'notLoggedIn' .$this->right_delimiter;
		}
		
		/**
		*	block element, which displays content only if user is Logged in
		*
		*/
		public function isLoggedIn($params, $content, &$smarty, &$repeat){
			if(!$repeat){ 
				// Define the has_permission_else tag, using Smarty's delimiters. 
			    $else = $smarty->left_delimiter . 'notLoggedIn' . $smarty->right_delimiter; 
			
			    // Use PHP's explode function to split the content at the point where the else tag occurs. 
			    $true_false = explode($else, $content, 2); 
			
			    // If explode has worked, the true_false array will contain the 'true' value in position zero. 
			    $true = (isset($true_false[0]) ? $true_false[0] : null); 
			
			    // If there is a 'false' value, this will be contained in position one. 
			    $false = (isset($true_false[1]) ? $true_false[1] : null); 
			
			    if(isset($_SESSION['user'])) 
			    { 
			       return $true; 
			    } 
			    else 
			    { 
			       return $false; 
			    }
			} 
			return null; 
		}
		
		/**
		*	block element, which displays content only if debug mode is on
		*
		*/
		public function debugMode($params, $content, &$smarty, &$repeat){
			if (!$repeat)
			{
				if (isset($_SESSION['debug_mode']) && $_SESSION['debug_mode']){
					return $content;
				}
			}	
		}
		
		/**
		*	block element, which displays content only if test mode is on
		*
		*/
		public function testMode($params, $content, &$smarty, &$repeat){
			if (!$repeat)
			{
				if (constant("TEST_MODE")){
					return $content;
				}
			}	
		}
		
		public function formatDate($params){
			return date($params["format"], $params["time"]);
		}
	}
?>